//
//  DateTools.m
//  easymarketing
//
//  Created by 韩海龙 on 15/1/19.
//  Copyright (c) 2015年 cubead. All rights reserved.
//

#import "DateTools.h"

@implementation DateTools

+ (NSDate *)getNSDate:(long long)time {
    const long long tempTime = time / 1000;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:tempTime];
    return date;
}

+(NSString *)getYearMonth:(long long)time{
    const long long tempTime = time / 1000;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:tempTime];
    NSString *result = [date stringWithFormat:@"yyyy-MM"];
    return result;
}

+(NSString *)getYearMonthDay:(long long)time{
    const long long tempTime = time / 1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:tempTime];
    
    NSString *result = [date stringWithFormat:@"yyyyMMdd"];
    return result;
}

+(NSString *)getChYearMonthDayHourMinite:(NSString *)time{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat : @"yyyyMMddHHmmss"];
    NSDate *dateTime = [formatter dateFromString:time];

    NSString *result = [dateTime stringWithFormat:@"yyyy年MM月dd日 HH点mm分"];
    return result;
}

+(NSString *)getChYearMonthDay:(NSString *)time{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat : @"yyyyMMddHHmmss"];
    NSDate *dateTime = [formatter dateFromString:time];
    
    NSString *result = [dateTime stringWithFormat:@"yyyy-MM-dd"];
    return result;
}

+(NSString *)getChYearMonthDay1:(NSString *)time{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat : @"yyyyMMdd"];
    NSDate *dateTime = [formatter dateFromString:time];
    
    NSString *result = [dateTime stringWithFormat:@"yyyy-MM-dd"];
    return result;
}

+(NSString *)getWeek:(long long)time{
    const long long tempTime = time / 1000;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:tempTime];
    NSArray *array = [NSArray arrayWithObjects:@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六", nil];
    long week = [date weekday];
    return [array objectAtIndex:week-1];
}

+(NSString *)getDayNum:(long long)time{
    const long long tempTime = time / 1000;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:tempTime];
    long day = [date day];
    return [NSString stringWithFormat:@"%ld号",day];
}

+(NSString *)getDay:(long long)time{
    const long long tempTime = time / 1000;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:tempTime];
    long day = [date day];
    if (day < 10) {
        return [NSString stringWithFormat:@"0%ld",day];
    }else{
        return [NSString stringWithFormat:@"%ld",day];
    }
}

+ (long long)getCurrentTicket{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    long long curtime = (long long)[date timeIntervalSince1970]*1000;
    return curtime;
}

+(long long)getDayFromNowTo:(long long)time{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    long long curtime = (long long)[date timeIntervalSince1970]*1000;
    return curtime/86400000 - time/86400000; // 86400000 = 24*60*60*1000(一天的微秒数)
}

+(NSString *)getHourMinute:(long long)time{
    const long long tempTime = time / 1000;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:tempTime];
    NSString *result = [date stringWithFormat:@"HH:mm"];
    return result;
}

+(NSString *)getDayTime:(long long)time {
    const long long tempTime = time / 1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:tempTime];
    
    NSString *result = [date stringWithFormat:@"YYYYMMdd hh:mm"];
    return result;
}

@end
